/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.select;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.databasefacade.select.functions.Function;
import org.databasefacade.translator.Translator;


/**
 * Array list that holds a collection
 * of columns.
 * 
 * @author Daniel
 *
 */
public class FunctionList
{
	private ArrayList functionList = new ArrayList();
	private ArrayList aliasList = new ArrayList();
	private HashMap functionHash = new HashMap();
	private HashMap functionSQL = new HashMap();
	private Translator translator;
	
	public FunctionList(Translator translator) {
		this.translator = translator;
	}
	
	public void addFunction(Function func, String colAlias)
	{
		add(func, colAlias);
	}
	
	public void removeFunction(int i)
	{
		String alias = (String) aliasList.get(i);
		functionList.remove(i);
		aliasList.remove(i);
		functionHash.remove(alias);
		functionSQL.remove(alias);
	}
	
	public void removeFunction(String alias)
	{ 
		for ( int i = 0; i < aliasList.size(); i++ ) {
			String lAlias = (String) aliasList.get(i);
			if ( alias.equals(lAlias) ) {
				removeFunction(i);
				break;
			}
		}
	}
	
	public Function getFunction(int i)
	{
		return (Function) functionList.get(i);
	}
	
	public Function getFunction(String alias)
	{
		return (Function) functionHash.get(alias);
	}
	
	public int size()
	{
		return functionList.size();
	}
	
	public Iterator getFunctionAliasNames()
	{
		return functionHash.entrySet().iterator();
	}

	public void clearFunctions()
	{
		functionList.clear();
		aliasList.clear();
		functionHash.clear();
		functionSQL.clear();
	}
	
	public String getSQL()
	{
		StringBuilder sql = new StringBuilder("");
		Iterator keys = functionSQL.keySet().iterator();
		while ( keys.hasNext() ) {
			String fsql = (String) functionSQL.get(keys.next());
			if ( sql.length() == 0 ) {
				sql.append(fsql);
			} else {
				sql.append(fsql);
			}
		}
		return sql.toString();
	}
	
	private void add(Function func, String colAlias)
	{
		String sql = func.getSQL(translator) + " " + colAlias;
		if ( !aliasList.contains(colAlias) ) {
			functionList.add(func);
			aliasList.add(colAlias);
		}
		functionHash.put(colAlias, func);
		functionSQL.put(colAlias, sql);
	}
}
